package com.wenan.ui.controls.menus
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.ContextMenuEvent;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.trace.Trace;
	
	import mx.controls.DataGrid;
	import mx.controls.List;
	import mx.controls.Menu;
	import mx.controls.listClasses.IListItemRenderer;
	import mx.core.UIComponent;
	import mx.events.MenuEvent;

	[Event(name="menuItemClickHandler",type="com.wenan.ui.controls.menus.MenuItemEvent")]
	
	/**
	 * 菜单控制类
	 * */
	public class PopupMenu extends EventDispatcher
	{
		/**
		 * 右键标示常量
		 * */
		public static const RIGHT_CLICK:String = "rightClick";
		
		/**
		 * 对象自身
		 * */
		public static var currentPopupMenu:PopupMenu = null;
		
		/**
		 * 可显示控件
		 * */
		private var _parent:DisplayObjectContainer;
		
		/**
		 * 菜单项集合
		 * */
		private var _items:MenuItemCollection;
		
		/**
		 * 可创建菜单
		 * */
		private var _innerMenu:Menu;
		
		/**
		 * 构建弹出菜单类
		 * */
		public function PopupMenu()
		{
			RightClickManager.regist();
			
			_items = new MenuItemCollection();
		}

		/**
		 * 获取可显示控件
		 * 
		 * @return 可显示控件对象
		 * */
		public function get parent():DisplayObjectContainer
		{
			invalidateSettings();
			
			return _parent;
		}

		public function set parent(value:DisplayObjectContainer):void
		{
			invalidateSettings();
			
			_parent = value;
		}

		/**
		 * 获取菜单项集合
		 * 
		 * @return 菜单项集合
		 * */
		public function get items():MenuItemCollection
		{
			invalidateSettings();
			
			return _items;
		}
		
		/**
		 * 指定位置弹出自定义右键菜单
		 * 
		 * @param x 横坐标数据
		 * @param y 纵坐标数据
		 * */
		public function popup(x:Number,y:Number):void
		{
			if (currentPopupMenu)
			{
				currentPopupMenu.removeMenu();
			}
			
			removeMenu();
			
			initMenu(x,y);
			
			currentPopupMenu = this;
		}

		/**
		 * 右键菜单的删除（包括：菜单的隐藏、监听的销毁）
		 * */
		private function removeMenu():void 
		{  
			if(_innerMenu!=null)  
			{  
				_innerMenu.hide();
				
				_innerMenu.removeEventListener(MenuEvent.ITEM_CLICK,menuItemClickHandler);  
				_innerMenu.removeEventListener(MenuEvent.ITEM_ROLL_OVER, menuItemOverHandler);
				_innerMenu.removeEventListener(MenuEvent.ITEM_ROLL_OUT, menuItemOutHandler);

				_innerMenu=null;  
			}  
		}
		
		/**
		 * 右键菜单的生成
		 * 
		 * @param x 横坐标数据
		 * @param y 纵坐标数据
		 * */
		private function initMenu(x:Number, y:Number):void
		{
			_innerMenu = Menu.createMenu(parent, createMenuItems(), false);
			
//			menu.iconField="itemIcon";//右键菜单的图标
			_innerMenu.labelField="label";  //右键菜单的名称 
			_innerMenu.variableRowHeight = true;     
			_innerMenu.addEventListener(MenuEvent.ITEM_CLICK, menuItemClickHandler);  //右键菜单的事件
			_innerMenu.addEventListener(MenuEvent.ITEM_ROLL_OVER, menuItemOverHandler);
			_innerMenu.addEventListener(MenuEvent.ITEM_ROLL_OUT, menuItemOutHandler);
			
			_innerMenu.show(x, y);  //显示右键菜单
		}
		
		/**
		 * 菜单项的生成
		 * 
		 * @return 存储了菜单项数据的数组
		 * */
		private function createMenuItems():Array 
		{
			return createMenuItemArray(_items);
		}
		
		/**
		 * 生成菜单项集合
		 * 
		 * @param items 菜单项数据集合
		 * */
		private function createMenuItemArray(items:MenuItemCollection):Array
		{
			var array:Array = new Array();
			
			var menuItem:MenuItem;
			var count:int = items.length;
			for(var i:int=0;i<count;i++)
			{
				menuItem = items.getAt(i);
				
				if (menuItem.items.length > 0)
				{
					menuItem.children = createMenuItemArray(menuItem.items);
				}
				
				array.push(menuItem);
			}
			
			return array;
		}
		
		/**
		 * 鼠标点击菜单项处理方法
		 * 
		 * @param event 事件对象
		 * */
		private function menuItemClickHandler(event:MenuEvent):void
		{
			dispatchEvent(new MenuItemEvent(MenuItemEvent.MENU_ITEM_CLICK, event.item as MenuItem));
		}
		
		/**
		 * 鼠标经过菜单项处理方法
		 * 
		 * @param event 事件对象
		 * */
		private function menuItemOverHandler(event:MenuEvent):void
		{
			dispatchEvent(new MenuItemEvent(MenuItemEvent.MENU_ITEM_OVER, event.item as MenuItem));
		}
		
		/**
		 * 鼠标离开菜单项处理方法
		 * 
		 * @param event 事件对象
		 * */
		private function menuItemOutHandler(event:MenuEvent):void
		{
			dispatchEvent(new MenuItemEvent(MenuItemEvent.MENU_ITEM_OUT, event.item as MenuItem));
		}
	
		/**
		 * 自定义右键菜单预处理
		 * */
		private function invalidateSettings():void
		{
			RightClickManager.init();
		}
	}
}